using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HHF.Models;
using System.Text;
using System.IO;

namespace HHF.Controllers
{
	public class VolunteerController : Controller
	{
		private readonly IVolunteerApplicationViewModelRepository volunteerapplicationviewmodelRepository;

		// If you are using Dependency Injection, you can delete the following constructor
		public VolunteerController()
			: this(new VolunteerApplicationViewModelRepository())
		{
		}

		public VolunteerController(IVolunteerApplicationViewModelRepository volunteerapplicationviewmodelRepository)
		{
			this.volunteerapplicationviewmodelRepository = volunteerapplicationviewmodelRepository;
		}

		//
		// GET: /Volunteer/
		[AuthorizeAdmin]
		public ViewResult Index()
		{
			return View(volunteerapplicationviewmodelRepository.All);
		}

		//
		// GET: /Volunteer/Details/5
		[AuthorizeAdmin]
		public ViewResult Details(int id)
		{
			return View(volunteerapplicationviewmodelRepository.Find(id));
		}

		//
		// GET: /Volunteer/Create
		public ActionResult Create()
		{
			return View();
		}

		//
		// POST: /Volunteer/Create
		[HttpPost]
		public ActionResult Create(VolunteerApplicationViewModel volunteerapplicationviewmodel)
		{
			if (ModelState.IsValid)
			{
				volunteerapplicationviewmodelRepository.InsertOrUpdate(volunteerapplicationviewmodel);
				volunteerapplicationviewmodelRepository.Save();

				if (!string.IsNullOrWhiteSpace(volunteerapplicationviewmodel.Email))
				{
					Email.Emailer.SendEmail(volunteerapplicationviewmodel.Email, "Thanks for Volunteering",
						"<p>Hello,</p><p>Your request has been received.  Thank you for your interest in volunteering with " + Constants.OrgName + ".</p><p>Sincerely,</p><p>The " + Constants.OrgName + " Team</p>");
				}

				string msg = "<p>A volunteer application was received:</p>";
				var props = volunteerapplicationviewmodel.GetType().GetProperties().ToList();
				props.ForEach((p) =>
				{
					msg += p.Name + " = ";
					var value = p.GetValue(volunteerapplicationviewmodel, null);
					msg += value != null ? value.ToString() : "";
				});

				Email.Emailer.SendEmail(Constants.DefaultEmail, "A Volunteer Application was Received", msg);

				return RedirectToAction("Index", "Home");
			}
			else
			{
				return View();
			}
		}

		//
		// GET: /Volunteer/Edit/5
		[AuthorizeAdmin]
		public ActionResult Edit(int id)
		{
			return View(volunteerapplicationviewmodelRepository.Find(id));
		}

		//
		// POST: /Volunteer/Edit/5
		[AuthorizeAdmin]
		[HttpPost]
		public ActionResult Edit(VolunteerApplicationViewModel volunteerapplicationviewmodel)
		{
			if (ModelState.IsValid)
			{
				volunteerapplicationviewmodelRepository.InsertOrUpdate(volunteerapplicationviewmodel);
				volunteerapplicationviewmodelRepository.Save();
				return RedirectToAction("Index");
			}
			else
			{
				return View();
			}
		}

		//
		// GET: /Volunteer/Delete/5
		[AuthorizeAdmin]
		public ActionResult Delete(int id)
		{
			return View(volunteerapplicationviewmodelRepository.Find(id));
		}

		//
		// POST: /Volunteer/Delete/5
		[AuthorizeAdmin]
		[HttpPost, ActionName("Delete")]
		public ActionResult DeleteConfirmed(int id)
		{
			volunteerapplicationviewmodelRepository.Delete(id);
			volunteerapplicationviewmodelRepository.Save();

			return RedirectToAction("Index");
		}

		[AuthorizeAdmin]
		public ActionResult Export()
		{
			var items = this.volunteerapplicationviewmodelRepository.All;
			var contents = new StringBuilder();

			var memoryStream = new MemoryStream();
			var writer = new StreamWriter(memoryStream);

			var firstItem = items.FirstOrDefault();

			// writer header row
			if (firstItem != null)
			{
				foreach (var property in firstItem.GetType().GetProperties())
				{
					//var value = Convert.ToString(property.GetValue(item, null));
					writer.Write("\"" + property.Name + "\",");
				}
				writer.WriteLine();
			}

			foreach (var item in items)
			{
				foreach (var property in item.GetType().GetProperties())
				{
					var value = Convert.ToString(property.GetValue(item, null));
					writer.Write("\"" + value + "\",");
				}
				writer.WriteLine();
			}
			//writer.WriteLine("abc,123,aaaa");
			//writer.WriteLine("111,222,333");
			//writer.WriteLine("444,555,666");
			writer.Flush();
			writer.BaseStream.Seek(0, SeekOrigin.Begin);

			return this.File(memoryStream, "text/csv", "volunteer-export.csv");
		}
	}
}

